{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {
    "slideshow": {
     "slide_type": "skip"
    }
   },
   "source": [
    "<table>\n",
    " <tr align=left><td><img align=left src=\"./images/CC-BY.png\">\n",
    " <td>Text provided under a Creative Commons Attribution license, CC-BY. All code is made available under the FSF-approved MIT license. (c) Kyle T. Mandli</td>\n",
    "</table>\n",
    "\n",
    "Note:  The presentation below largely follows part II in \"Finite Difference Methods for Ordinary and Partial Differential Equations\" by LeVeque (SIAM, 2007)."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "slideshow": {
     "slide_type": "skip"
    }
   },
   "outputs": [],
   "source": [
    "from __future__ import print_function\n",
    "\n",
    "%matplotlib inline\n",
    "import numpy\n",
    "import matplotlib.pyplot as plt"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "slideshow": {
     "slide_type": "slide"
    }
   },
   "source": [
    "# Numerical Solution to ODE Initial Value Problems - Part 2"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "slideshow": {
     "slide_type": "slide"
    }
   },
   "source": [
    "# Convergence\n",
    "\n",
    "We can think of an ODE method as providing a sequence of approximations $U_N$ where here $N$ is the number of time steps needed to reach the final time of interest $t_f$, in effect we are increasing the resolution of the method.  We then say that a method is convergent if this sequence converges to the true solution at the same time\n",
    "$$\n",
    "    \\lim_{N\\rightarrow \\infty} U_N = u(t_f).\n",
    "$$"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "slideshow": {
     "slide_type": "subslide"
    }
   },
   "source": [
    "We can also define this in a more familiar way in terms of $\\Delta t$ such that\n",
    "$$\n",
    "    n \\Delta t = t_f ~~~~ \\Rightarrow ~~~~ n = \\frac{t_f}{\\Delta t}\n",
    "$$\n",
    "so that our definition of convergence becomes\n",
    "$$\n",
    "    \\lim_{\\Delta t \\rightarrow 0} U_{\\Delta t} = u(t_f).\n",
    "$$\n",
    "In general for a method to be convergent it must be\n",
    " - **consistent** which as before meant that the local truncation error $T = \\mathcal{O}(\\Delta t^p)$ where $p > 0$,\n",
    " - **zero-stable** which implies that the sum total of the errors as $\\Delta t \\rightarrow 0$ is bounded and has the same order as $T$ (the truncation error) which we know goes to zero as $\\Delta t \\rightarrow 0$.\n",
    " "
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "slideshow": {
     "slide_type": "subslide"
    }
   },
   "source": [
    "### Example:  Forward Euler on a Linear Problem\n",
    "\n",
    "Consider the simple linear problem\n",
    "$$\n",
    "    \\frac{\\text{d}u}{\\text{d}t} = \\lambda u \\quad \\text{with} \\quad u(0) = u_0\n",
    "$$\n",
    "which we know has the solution $u(t) = u_0 e^{\\lambda t}$.  Applying Euler's method to this problem leads to \n",
    "$$\\begin{aligned}\n",
    "    U_{n+1} &= U_n + \\Delta t\\lambda U_n \\\\\n",
    "            &= (1 + \\Delta t \\lambda) U_n\n",
    "\\end{aligned}$$\n",
    "\n",
    "We also know the local truncation error is defined by\n",
    "$$\\begin{aligned}\n",
    "    T_n &= \\frac{1}{\\Delta t} \\left[ U_{n+1} - u_{n+1} \\right ] \\\\\n",
    "        &= \\frac{1}{\\Delta t} \\left[ (1 + \\lambda \\Delta t) u_n - u_{n+1} \\right ]\n",
    "\\end{aligned}$$\n",
    "which can be rearranged to find\n",
    "$$\n",
    "    u(t_{n+1}) = (1 + \\Delta t \\lambda) u(t_n) - \\Delta t T^n.\n",
    "$$\n",
    "Note that all values here are in terms of the exact solution where as the application of Euler's method is in terms of the approximate solution."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "slideshow": {
     "slide_type": "subslide"
    }
   },
   "source": [
    "Defining the global error as\n",
    "$$\n",
    "    E_{n} = u_n - U_n\n",
    "$$\n",
    "we can subtract the last two expressions to find\n",
    "$$\\begin{aligned}\n",
    "    u_{n+1} - U_{n+1} &= \\underbrace{(1 + \\Delta t \\lambda) u_n - \\Delta t T_n}_{u_{n+1}} - \\underbrace{(1 + \\Delta t \\lambda) U_n}_{U_{n+1}} \\\\\n",
    "    E_{n+1} &= (1 + \\Delta t \\lambda) (u_n - U_n) - \\Delta t T_n \\\\\n",
    "            &= (1 + \\Delta t \\lambda) E_n - \\Delta t T_n,\n",
    "\\end{aligned}$$\n",
    "a recursive definition for the global error that connects it to the local truncation error."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "slideshow": {
     "slide_type": "subslide"
    }
   },
   "source": [
    "The first few iterates of this look like\n",
    "$$\\begin{align}\n",
    "    E_n &= (1 + \\lambda \\Delta t) E_{n - 1} - \\Delta t T_{n-1} \\\\\n",
    "    &= (1 + \\lambda \\Delta t) \\left [(1 + \\lambda \\Delta t) E_{n - 2} - \\Delta t T_{n-2} \\right ] - \\Delta t T_{n-1} \\\\\n",
    "    &= (1 + \\lambda \\Delta t) \\left [(1 + \\lambda \\Delta t) \\left \\{(1 + \\lambda \\Delta t) E_{n - 3} - \\Delta t T_{n-3} \\right \\} - \\Delta t T_{n-2} \\right ] - \\Delta t T_{n-1}\n",
    "\\end{align}$$"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "slideshow": {
     "slide_type": "subslide"
    }
   },
   "source": [
    "Expanding this expression out backwards in time to $n=0$ leads to \n",
    "$$\n",
    "    E_n = (1 + \\Delta t \\lambda)^n E_0 - \\Delta t \\sum^n_{i=1} (1 + \\Delta t \\lambda)^{n-i} T_{i - 1}.\n",
    "$$\n",
    "\n",
    "Noting that (1 + \\Delta t \\lambda) we will bound this term by\n",
    "$$\n",
    "    e^{\\Delta t \\lambda} \\geq |1 + \\Delta t \\lambda|\n",
    "$$\n",
    "which then implies the term in the summation can be bounded by\n",
    "$$\n",
    "    |1 + \\Delta t \\lambda|^{n - i} \\leq e^{(n-i) \\Delta t |\\lambda|} \\leq e^{n \\Delta t |\\lambda||} \\leq e^{|\\lambda| t_f}\n",
    "$$"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "slideshow": {
     "slide_type": "subslide"
    }
   },
   "source": [
    "Using this expression in the expression for the global error we find\n",
    "$$\\begin{aligned}\n",
    "    E_n &= (1 + \\Delta t \\lambda)^n E_0 - \\Delta t \\sum^n_{i=1} (1 + \\Delta t \\lambda)^{n-i} T_{i - 1} \\\\\n",
    "    |E_n| &\\leq e^{|\\lambda| \\Delta t n} |E_0| + \\Delta t \\sum^n_{i=1} e^{|\\lambda| t_f} |T_{i - 1}| \\\\\n",
    "          &\\leq e^{|\\lambda| t_f} \\left(|E_0| + \\Delta t \\sum^n_{i=1} |T_{i - 1}|\\right) \\\\\n",
    "          &\\leq e^{|\\lambda| t_f} \\left(|E_0| + n \\Delta t \\max_{1 \\leq i \\leq n} |T_{i - 1}|\\right)\n",
    "\\end{aligned}$$"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "slideshow": {
     "slide_type": "subslide"
    }
   },
   "source": [
    "In other words the global error is bounded by the original global error and the maximum one-step error made multiplied by the number of time steps taken.  If $N = \\frac{t_f}{\\Delta t}$ as before and taking into account the local truncation error we can simplify this expression further to\n",
    "\n",
    "$$\n",
    "    |E_n| \\leq e^{|\\lambda| t_f} \\left[|E_0| + t_f \\left(\\frac{1}{2} \\Delta t |u''| + \\mathcal{O}(\\Delta t^2)\\right ) \\right]\n",
    "$$\n",
    "\n",
    "If we assume that we have used the correct initial condition $u_0$ then $E_0 \\rightarrow 0$ as $\\Delta t \\rightarrow 0$ and we see that the global error $E_n$ is bounded by $\\mathcal{O}(\\Delta t)$, the same as the local truncation error:\n",
    "$$\n",
    "    |E_n| \\leq e^{|\\lambda| t_f} t_f \\left(\\frac{1}{2} \\Delta t |u''| + \\mathcal{O}(\\Delta t^2)\\right ) = \\mathcal{O}(\\Delta t).\n",
    "$$"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "slideshow": {
     "slide_type": "slide"
    }
   },
   "source": [
    "## Absolute Stability\n",
    "Although zero-stability guarantees stability it is much more difficult to work with in general as the limit $\\Delta t \\rightarrow 0$ can be difficult to compute.  Instead we often consider a finite $\\Delta t$ and examine if the method is stable for this particular choice of $\\Delta t$.  This has the practical upside that it will also tell us what particular $\\Delta t$ will ensure that our method is indeed stable."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "slideshow": {
     "slide_type": "subslide"
    }
   },
   "source": [
    "### Example\n",
    "Consider the problem\n",
    "$$\n",
    "    u'(t) = \\lambda (u - \\cos t) - \\sin t ~~~~ \\text{with} ~~~~ u(0) = 1\n",
    "$$\n",
    "\n",
    "whose exact solution is \n",
    "$$\n",
    "    u(t) = \\cos t.\n",
    "$$\n",
    "\n",
    "We can compute an estimate for what $\\Delta t$ we need to use by examining the truncation error \n",
    "\n",
    "$$\\begin{aligned}\n",
    "    T &= \\frac{1}{2} \\Delta t u''(t) + \\mathcal{O}(\\Delta t^2) \\\\\n",
    "      &= -\\frac{1}{2} \\Delta t \\cos t + \\mathcal{O}(\\Delta t^2)\n",
    "\\end{aligned}$$\n",
    "\n",
    "and therefore\n",
    "$$\n",
    "    |E_n| \\leq \\Delta t \\max_{0 \\leq t \\leq t_f} |\\cos t| = \\Delta t.\n",
    "$$\n",
    "\n",
    "If we want a solution where $|E_n| < 10^{-3}$ then $\\Delta t \\approx 10^{-3}$.  Turning to the application of Euler's method lets apply this to the case where $\\lambda = -10$ and $\\lambda = -2100$."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "slideshow": {
     "slide_type": "skip"
    }
   },
   "outputs": [],
   "source": [
    "# Compare accuracy between Euler\n",
    "f = lambda t, lam, u: lam * (u - numpy.cos(t)) - numpy.sin(t)\n",
    "u_exact = lambda t: numpy.cos(t)\n",
    "\n",
    "t_f = 2.0\n",
    "# num_steps = [2**n for n in range(4, 9)]\n",
    "num_steps = [2**n for n in range(15,20)]\n",
    "delta_t = numpy.empty(len(num_steps))\n",
    "error_10 = numpy.empty(len(num_steps))\n",
    "error_2100 = numpy.empty(len(num_steps))\n",
    "\n",
    "for (i, N) in enumerate(num_steps):\n",
    "    t = numpy.linspace(0, t_f, N)\n",
    "    delta_t[i] = t[1] - t[0]\n",
    "    \n",
    "    # Compute Euler solution\n",
    "    U = numpy.empty(t.shape)\n",
    "    U[0] = 1.0\n",
    "    for (n, t_n) in enumerate(t[1:]):\n",
    "        U[n+1] = U[n] + delta_t[i] * f(t_n, -10.0, U[n])\n",
    "    error_10[i] = numpy.abs(U[-1] - u_exact(t_f)) / numpy.abs(u_exact(t_f))\n",
    "    \n",
    "    U = numpy.empty(t.shape)\n",
    "    U[0] = 1.0\n",
    "    for (n, t_n) in enumerate(t[1:]):\n",
    "        U[n+1] = U[n] + delta_t[i] * f(t_n, -2100.0, U[n])\n",
    "    error_2100[i] = numpy.abs(U[-1] - u_exact(t_f)) / numpy.abs(u_exact(t_f))\n",
    "    \n",
    "# Plot error vs. delta_t\n",
    "fig = plt.figure()\n",
    "fig.set_figwidth(fig.get_figwidth() * 2)\n",
    "axes = fig.add_subplot(1, 2, 1)\n",
    "\n",
    "axes.loglog(delta_t, error_10, 'bo', label='Forward Euler')\n",
    "\n",
    "order_C = lambda delta_x, error, order: numpy.exp(numpy.log(error) - order * numpy.log(delta_x))\n",
    "axes.loglog(delta_t, order_C(delta_t[1], error_10[1], 1.0) * delta_t**1.0, 'r--', label=\"1st Order\")\n",
    "\n",
    "axes.legend(loc=4)\n",
    "axes.set_title(\"Error - $\\lambda = 10$\")\n",
    "axes.set_xlabel(\"$\\Delta t$\")\n",
    "axes.set_ylabel(\"$|U(t_f) - u(t_f)|$\")\n",
    "\n",
    "axes = fig.add_subplot(1, 2, 2)\n",
    "\n",
    "axes.loglog(delta_t, error_2100, 'bo', label='Forward Euler')\n",
    "axes.loglog(delta_t, order_C(delta_t[1], error_2100[1], 1.0) * delta_t**1.0, 'r--', label=\"1st Order\")\n",
    "print(error_2100)\n",
    "\n",
    "axes.set_title(\"Error - $\\lambda = 2100$\")\n",
    "axes.set_xlabel(\"$\\Delta t$\")\n",
    "axes.set_ylabel(\"$|U(t_f) - u(t_f)|$\")\n",
    "\n",
    "plt.show()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "slideshow": {
     "slide_type": "subslide"
    }
   },
   "source": [
    "So what went wrong with $\\lambda = -2100$, the global error should go as\n",
    "\n",
    "$$\n",
    "    E_n = \\mathcal{O}(\\Delta t)?\n",
    "$$"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "slideshow": {
     "slide_type": "subslide"
    }
   },
   "source": [
    "If $\\Delta t \\approx 10^{-3}$ then for the case $\\lambda = -10$ the previous global error is multiplied by\n",
    "\n",
    "$$1 + 10^{-3} \\cdot -10 = 0.99$$\n",
    "\n",
    "which means the contribution from $E^n$ will slowly decrease as we take more time steps.  For the other case we have\n",
    "\n",
    "$$1 + 10^{-3} \\cdot -2100 = -1.1$$\n",
    "\n",
    "which means that for this $\\Delta t$ the error made in previous time steps will grow!  For this not to happen we would have to have $\\Delta t < 1 / 2100$ which would lead to convergence again."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "slideshow": {
     "slide_type": "subslide"
    }
   },
   "source": [
    "### Absolute Stability of the Forward Euler Method\n",
    "\n",
    "Consider again the simple test problem $u'(t) = \\lambda u$.  We know from before that applying Euler's method to this problem leads to an update of the form\n",
    "\n",
    "$$U_{n+1} = (1 + \\Delta t \\lambda) U_n.$$\n",
    "\n",
    "As may have been clear from the last example, we know that if\n",
    "\n",
    "$$|1 + \\Delta t \\lambda| \\leq 1$$\n",
    "\n",
    "that the method will be stable, this is called **absolute stability**.  Note that the product of $\\Delta t \\lambda$ is what matters here and often we consider a **region of absolute stability** on the complex plane defined by the equation outlined where now $z = \\Delta t \\lambda$.  This allows the values of $\\lambda$ to be complex, which can be an important case to consider, especially for systems of equations where the $\\lambda$s are identified as the eigenvalues."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "slideshow": {
     "slide_type": "skip"
    }
   },
   "outputs": [],
   "source": [
    "# Plot the region of absolute stability for Forward Euler\n",
    "fig = plt.figure()\n",
    "axes = fig.add_subplot(1, 1, 1)\n",
    "\n",
    "t = numpy.linspace(0.0, 2.0 * numpy.pi, 100)\n",
    "\n",
    "axes.fill(numpy.cos(t) - 1.0, numpy.sin(t), color=(255.0/255.0,145.0/255.0,0/255.0,1.0))\n",
    "axes.plot([-3, 3],[0.0, 0.0],'k--')\n",
    "axes.plot([0.0, 0.0],[-3, 3],'k--')\n",
    "axes.set_xlim((-3, 3.0))\n",
    "axes.set_ylim((-3,3))\n",
    "axes.set_aspect('equal')\n",
    "\n",
    "axes.set_title(\"Absolute Stability Region for Forward Euler\")\n",
    "plt.show()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "slideshow": {
     "slide_type": "slide"
    }
   },
   "source": [
    "### Characteristic Polynomials and Linear Difference Equations\n",
    "\n",
    "To help us analyze stability regions we will take a small aside and consider linear difference equations and their solutions.  Say we wanted to solve\n",
    "\n",
    "$$\\sum^r_{j=0} \\alpha_j U_{n+j} = 0$$\n",
    "\n",
    "given initial conditions $U_0, U_1, \\ldots, U_{r-1}$.  This expression has a solution in the general form $U_n = \\xi^n$.  "
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "slideshow": {
     "slide_type": "subslide"
    }
   },
   "source": [
    "Plugging this into the equation we have\n",
    "\n",
    "$$\\sum^r_{j=0} \\alpha_j \\xi^{n+j} = 0$$\n",
    "\n",
    "which simplifies to\n",
    "\n",
    "$$\\sum^r_{j=0} \\alpha_j \\xi^j = 0 $$\n",
    "\n",
    "by dividing by $\\xi^n$.  If $\\xi$ then is a root of the polynomial\n",
    "\n",
    "$$\\rho(\\xi) \\equiv \\sum^r_{j=0} \\alpha_j \\xi^j$$\n",
    "\n",
    "then $\\xi$ solves the original difference equation."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "slideshow": {
     "slide_type": "subslide"
    }
   },
   "source": [
    "### General Stability Regions for Linear Methods\n",
    "\n",
    "Going back to our stability region discussions we can consider any of the methods we outlined in the last lecture applied to our test problem as a general linear difference equation\n",
    "$$\n",
    "    \\sum^r_{j=0} \\alpha_j U_{n+j} = \\Delta t \\sum^r_{j=0} \\beta_j \\lambda U_{n+j}\n",
    "$$\n",
    "which can be written as \n",
    "$$\n",
    "    \\sum^r_{j=0} (\\alpha_j - \\beta_j \\Delta t \\lambda) U_{n+j} = 0\n",
    "$$\n",
    "or using our notation of $z = \\Delta t \\lambda$ we have\n",
    "$$\n",
    "    \\sum^r_{j=0} (\\alpha_j - \\beta_j z) U_{n+j} = 0.\n",
    "$$\n",
    "This has a similar form to the linear difference equations considered above! "
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "slideshow": {
     "slide_type": "subslide"
    }
   },
   "source": [
    "Let\n",
    "$$\n",
    "    \\rho(\\xi) = \\sum^r_{j=0} \\alpha_j \\xi^j\n",
    "$$\n",
    "and \n",
    "$$\n",
    "    \\sigma(\\xi) = \\sum^r_{j=0} \\beta_j \\xi^j\n",
    "$$\n",
    "we can write the expression above as\n",
    "$$\n",
    "    \\pi(\\xi, z) = \\rho(\\xi) - z \\sigma(\\xi)\n",
    "$$\n",
    "called the **stability polynomial** of the method.  "
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "slideshow": {
     "slide_type": "subslide"
    }
   },
   "source": [
    "It turns out that if the roots $\\xi_i$ of $\\pi(\\xi, z)$ satisfy\n",
    "$$\n",
    "    |\\xi_i| \\leq 1\n",
    "$$\n",
    "then the multi-step method is zero-stable.  Roughly you can think of these roots as multiplying the past truncation errors so that if they are less than or equal to 1 they do not grow in magnitude.  We then define the region of absolute stability as the values for $z$ for which this is true.  This approach can also be applied to one-step methods as a special case."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "slideshow": {
     "slide_type": "subslide"
    }
   },
   "source": [
    "### Example:  Forward Euler's Method\n",
    "\n",
    "Examining forward Euler's method we have\n",
    "\n",
    "$$\\begin{aligned}\n",
    "    0 &= U_{n+1} - U_n - \\Delta t \\lambda U_n \\\\\n",
    "    &= U_{n+1} - U_n (1 + \\Delta t \\lambda)\\\\\n",
    "    &= \\xi - 1 (1 + z)\\\\\n",
    "    &=\\pi(\\xi, z)\n",
    "\\end{aligned}$$\n",
    "\n",
    "whose root is $\\xi = 1 + z$ and we have re-derived the stability region we had found before."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "slideshow": {
     "slide_type": "skip"
    }
   },
   "outputs": [],
   "source": [
    "# Plot the region of absolute stability for Forward Euler\n",
    "fig = plt.figure()\n",
    "axes = fig.add_subplot(1, 1, 1)\n",
    "\n",
    "t = numpy.linspace(0.0, 2.0 * numpy.pi, 100)\n",
    "\n",
    "axes.fill(numpy.cos(t) - 1.0, numpy.sin(t), color=(255.0/255.0,145.0/255.0,0/255.0,1.0))\n",
    "axes.plot([-3, 3],[0.0, 0.0],'k--')\n",
    "axes.plot([0.0, 0.0],[-3, 3],'k--')\n",
    "axes.set_xlim((-3, 3.0))\n",
    "axes.set_ylim((-3,3))\n",
    "axes.set_aspect('equal')\n",
    "\n",
    "axes.set_title(\"Absolute Stability Region for Forward Euler\")\n",
    "plt.show()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "slideshow": {
     "slide_type": "subslide"
    }
   },
   "source": [
    "### Absolute Stability of the backward Euler Method\n",
    "\n",
    "Now try this on backward Euler.\n",
    "\n",
    "$$\n",
    "    U_{n+1} = U_n + \\Delta t f(t_{n+1}, U_{n+1}).\n",
    "$$"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "slideshow": {
     "slide_type": "subslide"
    }
   },
   "source": [
    "The backward version of Euler's method is defined as\n",
    "\n",
    "$$\n",
    "    U_{n+1} = U_n + \\Delta t f(t_{n+1}, U_{n+1}).\n",
    "$$\n",
    "\n",
    "If we again consider the test problem from before we find that\n",
    "\n",
    "$$\\begin{aligned}\n",
    "    0 &= U_{n+1} (1 - \\Delta t \\lambda) - U_n \\\\\n",
    "    &= \\xi (1 - z) - 1\n",
    "\\end{aligned}$$\n",
    "\n",
    "which has the root $\\xi = \\frac{1}{1 - z}$.  We then have\n",
    "\n",
    "$$\\left|\\frac{1}{1-z}\\right| \\leq 1 \\leftrightarrow |1 - z| \\geq 1$$\n",
    "\n",
    "so in fact the stability region encompasses the entire complex plane except for a circle centered at $(1, 0)$ of radius 1 implying that the backward Euler method is in fact stable for any choice of $\\Delta t$."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# Plot the region of absolute stability for Backward Euler\n",
    "fig = plt.figure()\n",
    "axes = fig.add_subplot(1, 1, 1)\n",
    "\n",
    "t = numpy.linspace(0.0, 2.0 * numpy.pi, 100)\n",
    "\n",
    "axes.set_facecolor((255.0/255.0,145.0/255.0,0/255.0,1.0))\n",
    "axes.fill(numpy.cos(t) + 1.0, numpy.sin(t), 'w')\n",
    "axes.plot([-3, 3],[0.0, 0.0],'k--')\n",
    "axes.plot([0.0, 0.0],[-3, 3],'k--')\n",
    "# axes.set_xlim((-3, 1))\n",
    "axes.set_ylim((-2,2))\n",
    "axes.set_aspect('equal')\n",
    "\n",
    "axes.set_title(\"Absolute Stability Region for Backward Euler\")\n",
    "plt.show()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "slideshow": {
     "slide_type": "slide"
    }
   },
   "source": [
    "## Application to Stiff ODEs\n",
    "\n",
    "Consider again the ODE we examined before\n",
    "\n",
    "$$u'(t) = \\lambda (u - \\cos t) - \\sin t$$\n",
    "\n",
    "except this time with general initial condition $u(t_0) = \\eta$.  What happens to solutions that are slightly different from $\\eta = 1$ or $t_0 = 0$?  The general solution of the ODE is\n",
    "\n",
    "$$u(t) = e^{\\lambda (t - t_0)} (\\eta - \\cos t_0)) + \\cos t$$."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "slideshow": {
     "slide_type": "skip"
    }
   },
   "outputs": [],
   "source": [
    "# Plot \"hairy\" solutions to the ODE\n",
    "u = lambda t_0, eta, lam, t: numpy.exp(lam * (t - t_0)) * (eta - numpy.cos(t_0)) + numpy.cos(t)\n",
    "\n",
    "for lam in [1, 10]:\n",
    "    fig = plt.figure()\n",
    "    axes = fig.add_subplot(1, 1, 1)\n",
    "    for eta in numpy.linspace(-1, 1, 10):\n",
    "        for t_0 in numpy.linspace(0.0, 9.0, 10):\n",
    "            t = numpy.linspace(t_0,10.0,100)\n",
    "            axes.plot(t, u(t_0, eta, lam, t),'b')\n",
    "    t = numpy.linspace(0.0,10.0,100)\n",
    "    axes.plot(t, numpy.cos(t), 'r', linewidth=5)\n",
    "    axes.set_ylim((-2, 2))\n",
    "        \n",
    "    axes.set_title(\"Perturbed Solutions $\\lambda = %s$\" % lam)\n",
    "    axes.set_xlabel('$t$')\n",
    "    axes.set_ylabel('$u(t)$')\n",
    "axes.set_ylim((-2, 2))\n",
    "plt.show()\n",
    "        "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "slideshow": {
     "slide_type": "skip"
    }
   },
   "outputs": [],
   "source": [
    "# Plot \"inverse hairy\" solutions to the ODE\n",
    "u = lambda t_0, eta, lam, t: numpy.exp(lam * (t - t_0)) * (eta - numpy.cos(t_0)) + numpy.cos(t)\n",
    "\n",
    "num_steps = 10\n",
    "error = numpy.ones(num_steps) * 1.0\n",
    "t_hat = numpy.linspace(0.0, 10.0, num_steps + 1)\n",
    "t_whole = numpy.linspace(0.0, 10.0, 1000)\n",
    "\n",
    "fig = plt.figure()\n",
    "axes = fig.add_subplot(1, 1, 1)\n",
    "eta = 1.0\n",
    "lam = 0.1\n",
    "\n",
    "for n in range(1,num_steps):\n",
    "    t = numpy.linspace(t_hat[n-1], t_hat[n], 100)\n",
    "    U = u(t_hat[n-1], eta, lam, t)\n",
    "    axes.plot(t, U, 'b')\n",
    "    axes.plot(t_whole, u(t_hat[n-1], eta, lam, t_whole),'b--')\n",
    "    axes.plot([t[-1], t[-1]], (U[-1], U[-1] + -1.0**n * error[n]), 'r')\n",
    "    eta = U[-1] + -1.0**n * error[n]\n",
    "\n",
    "t = numpy.linspace(0.0, 10.0, 100)\n",
    "axes.plot(t, numpy.cos(t), 'g')\n",
    "\n",
    "axes.set_title(\"Perturbed Solutions $\\lambda = %s$\" % lam)\n",
    "axes.set_xlabel('$t$')\n",
    "axes.set_ylabel('$u(t)$')\n",
    "axes.set_ylim((-10,10))\n",
    "plt.show()\n",
    "        "
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "slideshow": {
     "slide_type": "subslide"
    }
   },
   "source": [
    "### Example:  Chemical systems\n",
    "\n",
    "Consider the transition of a chemical $A$ to a chemical $C$ through the process\n",
    "\n",
    "$$A \\overset{K_1}{\\rightarrow} B \\overset{K_2}{\\rightarrow} C.$$\n",
    "\n",
    "If we let\n",
    "\n",
    "$$\\vec{u} = \\begin{bmatrix} [A] \\\\ [B] \\\\ [C] \\end{bmatrix}$$\n",
    "\n",
    "then we can model this simple chemical reaction with the system of ODEs\n",
    "\n",
    "$$\\frac{\\text{d} \\vec{u}}{\\text{d} t} = \n",
    "\\begin{bmatrix}\n",
    "    -K_1 & 0 & 0 \\\\\n",
    "    K_1 & -K_2 & 0 \\\\\n",
    "    0 & K_2 & 0\n",
    "\\end{bmatrix} \\vec{u}$$\n",
    "\n",
    "The solution of this system is of the form\n",
    "\n",
    "$$u_j(t) = c_{j1} e^{-K_1 t} + c_{j2}e^{-K_2 t} + c_{j3}$$"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "slideshow": {
     "slide_type": "skip"
    }
   },
   "outputs": [],
   "source": [
    "# Solve the chemical systems example\n",
    "# Problem parameters\n",
    "K_1 = 3\n",
    "K_2 = 1\n",
    "N = 128\n",
    "K_1 = 30.0\n",
    "K_2 = 1.0\n",
    "\n",
    "A = numpy.array([[-K_1, 0, 0], [K_1, -K_2, 0], [0, K_2, 0]])\n",
    "f = lambda u: numpy.dot(A, u)\n",
    "\n",
    "t = numpy.linspace(0.0, 2.0, N)\n",
    "delta_t = t[1] - t[0]\n",
    "\n",
    "U = numpy.empty((t.shape[0], 3))\n",
    "U[0, :] = [2.5, 5.0, 2.0]\n",
    "\n",
    "for n in range(t.shape[0] - 1):\n",
    "    U[n+1, :] = U[n, :] / (1.0 - delta_t * f(U[n, :]))\n",
    "    \n",
    "fig = plt.figure()\n",
    "axes = fig.add_subplot(1, 1, 1)\n",
    "\n",
    "axes.plot(t, U)\n",
    "axes.legend([\"[A]\", \"[B]\", \"[C]\"])\n",
    "axes.set_title(\"Chemical System\")\n",
    "axes.set_xlabel(\"$t$\")\n",
    "axes.set_title(\"$[A], [B], [C]$\")\n",
    "axes.set_ylim((0.0, 10.))\n",
    "axes.set_xlim((0.0, 2.0))\n",
    "\n",
    "plt.show()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "slideshow": {
     "slide_type": "subslide"
    }
   },
   "source": [
    "### What is stiffness?\n",
    "\n",
    "In general a **stiff** ODE is one where $u'(t) \\ll f'(t, u)$.  For systems of ODEs the **stiffness ratio**\n",
    "\n",
    "$$\\frac{\\max_p |\\lambda_p|}{\\min_p |\\lambda_p|}$$\n",
    "\n",
    "can be used to characterize the stiffness of the system.  In our last example this ratio was $K_1 / K_2$ if $K_1 > K_2$.  As we increased this ratio we observed that the numerical method became unstable only a reduction in $\\Delta t$ lead to stable solution again.  For explicit time step methods this is problematic as the reduction of the time step for only one of the species leads to very expensive evaluations.  For example, forward Euler has the stability criteria\n",
    "\n",
    "$$|1 + \\Delta t \\lambda| < 1$$\n",
    "\n",
    "where $\\lambda$ will have to be the maximum eigenvalue of the system.  "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "slideshow": {
     "slide_type": "skip"
    }
   },
   "outputs": [],
   "source": [
    "# Plot the region of absolute stability for Forward Euler\n",
    "fig = plt.figure()\n",
    "axes = fig.add_subplot(1, 1, 1)\n",
    "\n",
    "t = numpy.linspace(0.0, 2.0 * numpy.pi, 100)\n",
    "\n",
    "# K_1 = 30.0\n",
    "# K_2 = 1.0\n",
    "K_1 = 3.0\n",
    "K_2 = 1.0\n",
    "delta_t = 1.0\n",
    "eigenvalues = [-K_1, -K_2]\n",
    "\n",
    "axes.fill(numpy.cos(t) - 1.0, numpy.sin(t), color=(255.0/255.0,145.0/255.0,0/255.0,1.0))\n",
    "for lam in eigenvalues:\n",
    "    print(lam * delta_t)\n",
    "    axes.plot(lam * delta_t, 0.0, 'ko')\n",
    "axes.plot([-3, 3],[0.0, 0.0],'k--')\n",
    "axes.plot([0.0, 0.0],[-3, 3],'k--')\n",
    "# axes.set_xlim((-3, 1))\n",
    "axes.set_ylim((-2,2))\n",
    "axes.set_aspect('equal')\n",
    "\n",
    "axes.set_title(\"Absolute Stability Region for Forward Euler\")\n",
    "plt.show()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "slideshow": {
     "slide_type": "subslide"
    }
   },
   "source": [
    "### A-Stability\n",
    "What if we could expand the absolute stability region to encompass more of the left-half plane or even better, all of it.  A method that has this property is called **A-stable**.  We have already seen one example of this with backward Euler which as a stability region of\n",
    "\n",
    "$$|1 - z| \\geq 1$$\n",
    "\n",
    "which covers the full left-half plane.  It turns out that for linear multi-step methods a theorem by Dahlquist proves that there are no LMMs that satisfies the A-stability criterion beyond second order (trapezoidal rule).  There are higher-order Runge-Kutta methods do however.\n",
    "\n",
    "Perhaps this is too restrictive though.  Often large eigenvalues for systems (for instance coming from a PDE discretization for the heat equation) lie completely on the real line.  If the stability region can encompass as much of the real line as possible while leaving out the rest of the left-half plane we can possibly get a more efficient method.  There are a number of methods that can be constructed that have this property but are higher-order."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "slideshow": {
     "slide_type": "skip"
    }
   },
   "outputs": [],
   "source": [
    "# Plot the region of absolute stability for Backward Euler\n",
    "fig = plt.figure()\n",
    "axes = fig.add_subplot(1, 1, 1)\n",
    "\n",
    "t = numpy.linspace(0.0, 2.0 * numpy.pi, 100)\n",
    "\n",
    "K_1 = 3.0\n",
    "K_2 = 1.0\n",
    "delta_t = 1.0\n",
    "eigenvalues = [-K_1, -K_2]\n",
    "\n",
    "axes.set_facecolor((255.0/255.0,145.0/255.0,0/255.0,1.0))\n",
    "axes.fill(numpy.cos(t) + 1.0, numpy.sin(t), 'w')\n",
    "for lam in eigenvalues:\n",
    "    print(lam * delta_t)\n",
    "    axes.plot(lam * delta_t, 0.0, 'ko')\n",
    "axes.plot([-3, 3],[0.0, 0.0],'k--')\n",
    "axes.plot([0.0, 0.0],[-3, 3],'k--')\n",
    "# axes.set_xlim((-3, 1))\n",
    "axes.set_ylim((-2,2))\n",
    "axes.set_aspect('equal')\n",
    "\n",
    "axes.set_title(\"Absolute Stability Region for Backward Euler\")\n",
    "plt.show()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "slideshow": {
     "slide_type": "slide"
    }
   },
   "source": [
    "# L-Stability\n",
    "\n",
    "It turns out not all A-stable methods are alike.  Consider the backward Euler method and the trapezoidal method defined by\n",
    "\n",
    "$$\\frac{U_{n+1} - U_n}{\\Delta t} = \\frac{1}{2}(f(U_n) + f(U_{n+1})$$\n",
    "\n",
    "whose stability polynomial is\n",
    "\n",
    "$$\\begin{aligned}\n",
    "    0 &= U_{n+1} - U_n - \\Delta t \\frac{1}{2} (\\lambda U_n + \\lambda U_{n+1}) \\\\\n",
    "      &= U_{n+1}\\left(1 - \\frac{1}{2} \\Delta t \\lambda \\right ) - U_n \\left(1 + \\frac{1}{2}\\Delta t \\lambda \\right) \\\\\n",
    "      &= \\left(\\xi - \\frac{1 + \\frac{1}{2}z}{1 - \\frac{1}{2} z}\\right) \\left(1 - \\frac{1}{2} z \\right )\\\\\n",
    "\\end{aligned}$$\n",
    "which shows that it is A-stable.  Lets apply both these methods to a problem we have seen before and see what happens."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "slideshow": {
     "slide_type": "skip"
    }
   },
   "outputs": [],
   "source": [
    "# Compare accuracy between Euler\n",
    "f = lambda t, lam, u: lam * (u - numpy.cos(t)) - numpy.sin(t)\n",
    "u_exact = lambda t_0, eta, lam, t: numpy.exp(lam * (t - t_0)) * (eta - numpy.cos(t_0)) + numpy.cos(t)\n",
    "\n",
    "t_0 = 0.0\n",
    "t_f = 1.0\n",
    "eta = 2.0\n",
    "lam = -1e6\n",
    "\n",
    "num_steps = [10, 20, 40, 50]\n",
    "# num_steps = numpy.arange(100, 1000, 100)\n",
    "\n",
    "delta_t = numpy.empty(len(num_steps))\n",
    "error_euler = numpy.empty(len(num_steps))\n",
    "error_trap = numpy.empty(len(num_steps))\n",
    "\n",
    "for (i, N) in enumerate(num_steps):\n",
    "    t = numpy.linspace(0, t_f, N)\n",
    "    delta_t[i] = t[1] - t[0]\n",
    "    u = u_exact(t_0, eta, lam, t_f)\n",
    "    \n",
    "    # Compute Euler solution\n",
    "    U_euler = numpy.empty(t.shape)\n",
    "    U_euler[0] = eta\n",
    "    for (n, t_n) in enumerate(t[1:]):\n",
    "        U_euler[n+1] = (U_euler[n] - lam * delta_t[i] * numpy.cos(t_n) - delta_t[i] * numpy.sin(t_n)) / (1.0 - lam * delta_t[i])\n",
    "    error_euler[i] = numpy.abs(U_euler[-1] - u) / numpy.abs(u)\n",
    "    \n",
    "    # Compute using trapezoidal\n",
    "    U_trap = numpy.empty(t.shape)\n",
    "    U_trap[0] = eta\n",
    "    for (n, t_n) in enumerate(t[1:]):\n",
    "        U_trap[n+1] = (U_trap[n] + delta_t[i] * 0.5 * f(t_n, lam, U_trap[n]) - 0.5 * lam * delta_t[i] * numpy.cos(t_n) - 0.5 * delta_t[i] * numpy.sin(t_n)) / (1.0 - 0.5 * lam * delta_t[i])\n",
    "    error_trap[i] = numpy.abs(U_trap[-1] - u)\n",
    "    \n",
    "# Plot error vs. delta_t\n",
    "fig = plt.figure()\n",
    "fig.set_figwidth(fig.get_figwidth() * 2)\n",
    "axes = fig.add_subplot(1, 2, 1)\n",
    "axes.plot(t, U_euler, 'ro-')\n",
    "axes.plot(t, u_exact(t_0, eta, lam, t),'k')\n",
    "\n",
    "axes = fig.add_subplot(1, 2, 2)\n",
    "axes.loglog(delta_t, error_euler, 'bo')\n",
    "order_C = lambda delta_x, error, order: numpy.exp(numpy.log(error) - order * numpy.log(delta_x))\n",
    "axes.loglog(delta_t, order_C(delta_t[1], error_euler[1], 1.0) * delta_t**1.0, 'r--', label=\"1st Order\")\n",
    "axes.loglog(delta_t, order_C(delta_t[1], error_euler[1], 2.0) * delta_t**2.0, 'b--', label=\"2nd Order\")\n",
    "\n",
    "axes.legend(loc=4)\n",
    "axes.set_title(\"Comparison of Errors for Backwards Euler\")\n",
    "axes.set_xlabel(\"$\\Delta t$\")\n",
    "axes.set_ylabel(\"$|U(t_f) - u(t_f)|$\")\n",
    "\n",
    "# Plots for trapezoid\n",
    "fig = plt.figure()\n",
    "fig.set_figwidth(fig.get_figwidth() * 2)\n",
    "axes = fig.add_subplot(1, 2, 1)\n",
    "axes.plot(t, U_trap, 'ro-')\n",
    "axes.plot(t, u_exact(t_0, eta, lam, t),'k')\n",
    "\n",
    "axes = fig.add_subplot(1, 2, 2)\n",
    "axes.loglog(delta_t, error_trap, 'bo', label='Forward Euler')\n",
    "order_C = lambda delta_x, error, order: numpy.exp(numpy.log(error) - order * numpy.log(delta_x))\n",
    "axes.loglog(delta_t, order_C(delta_t[1], error_trap[1], 1.0) * delta_t**1.0, 'r--', label=\"1st Order\")\n",
    "axes.loglog(delta_t, order_C(delta_t[1], error_trap[1], 2.0) * delta_t**2.0, 'b--', label=\"2nd Order\")\n",
    "\n",
    "axes.legend(loc=4)\n",
    "axes.set_title(\"Comparison of Errors for Trapezoidal Rule\")\n",
    "axes.set_xlabel(\"$\\Delta t$\")\n",
    "axes.set_ylabel(\"$|U(t_f) - u(t_f)|$\")\n",
    "\n",
    "plt.show()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "slideshow": {
     "slide_type": "subslide"
    }
   },
   "source": [
    "It turns out that if we look at a one-step method and define the following ratio\n",
    "\n",
    "$$U_{n+1} = R(z) U_n$$\n",
    "\n",
    "we can define another form of stability, called **L-stable**, where we require that the method is A-stable and that\n",
    "\n",
    "$$\\lim_{z \\rightarrow \\infty} |R(z)| = 0.$$\n",
    "\n",
    "Turns out that backwards Euler is L-stable while trapezoidal rule is not."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "slideshow": {
     "slide_type": "slide"
    }
   },
   "source": [
    "## Backward Differencing Formulas\n",
    "\n",
    "A class of LMM methods that are useful for stiff ODE problems are the backward difference formula (BDF) methods which have the form\n",
    "\n",
    "$$\\alpha_0 U_n + \\alpha_1 U_{n+1} + \\cdots + \\alpha_r U_{n+r} = \\Delta \\beta_r f(U_{n+r})$$\n",
    "\n",
    "These methods can be derived directly from backwards finite differences from the point $U_{n+r}$ and the rest of the points back in time.  One can then derive r-step methods that are rth-order accurate this way.  Some of the methods are \n",
    "\n",
    "$$\\begin{aligned}\n",
    "    r = 1:& & U_{n+1} - U_n = \\Delta t f(U_{n+1}) \\\\\n",
    "    r = 2:& &3 U_{n+2} - 4 U_{n+1} + U_n = 2 \\Delta t f(U_{n+1}) \\\\\n",
    "    r = 3:& &11U_{n+3} - 18U_{n+2} + 9U_{n+1} - 2 U_n = 6 \\Delta t f(U_{n+3}) \\\\\n",
    "    r = 4:& &25 U_{n+4} - 48 U_{n+3} +36 U_{n+2} -16 U_{n+1} +3 U_n = 12 \\Delta t f(U_{n+4})\n",
    "\\end{aligned}$$"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "slideshow": {
     "slide_type": "slide"
    }
   },
   "source": [
    "## Plotting Stability Regions\n",
    "\n",
    "The most direct way to plot stability regions on the complex plane is to use a `pcolor` plot and color the value of $\\xi$ at every point in some region of $\\mathbb C$.  Here we plot these functions and draw a contour at the value of $\\xi = 1$."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "x = numpy.linspace(-2, 2, 100)\n",
    "y = numpy.linspace(-2, 2, 100)\n",
    "X, Y = numpy.meshgrid(x, y)\n",
    "Z = X + 1j * Y\n",
    "\n",
    "fig, axes = plt.subplots(1, 2)\n",
    "fig.set_figwidth(fig.get_figwidth() * 2)\n",
    "pcolor_plot = axes[0].pcolor(X, Y, numpy.abs(1 + Z), vmin=0.0, vmax=1.0, cmap=plt.get_cmap('Greens_r'))\n",
    "axes[0].contour(X, Y, numpy.abs(1 + Z), levels=[1.0])\n",
    "axes[0].plot(x, numpy.zeros(x.shape),'k--')\n",
    "axes[0].plot(numpy.zeros(y.shape), y,'k')\n",
    "axes[0].set_aspect('equal')\n",
    "axes[0].set_title(\"Forward Euler\")\n",
    "axes[0].set_xlabel(r\"$\\Re(\\xi)$\")\n",
    "axes[0].set_ylabel(r\"$\\Im(\\xi)$\")\n",
    "\n",
    "pcolor_plot = axes[1].pcolor(X, Y, numpy.abs(1.0 / (1.0 - Z)), vmin=0.0, vmax=1.0, cmap=plt.get_cmap('Greens_r'))\n",
    "axes[1].contour(X, Y, numpy.abs(1.0 / (1.0 - Z)), levels=[1.0])\n",
    "axes[1].plot(x, numpy.zeros(x.shape),'k--')\n",
    "axes[1].plot(numpy.zeros(y.shape), y,'k')\n",
    "axes[1].set_aspect('equal')\n",
    "axes[1].set_title(\"Backward Euler\")\n",
    "axes[1].set_xlabel(r\"$\\Re(\\xi)$\")\n",
    "axes[1].set_ylabel(r\"$\\Im(\\xi)$\")\n",
    "\n",
    "cbar = fig.colorbar(pcolor_plot)\n",
    "cbar.set_label(r\"$|\\xi|$\")\n",
    "plt.show()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Plotting the contours\n",
    "\n",
    "If we think of the roots of the stability polynomial $\\xi_j$ as complex numbers and write them in exponential form \n",
    "\n",
    "$$\\xi_j = |\\xi_j| e^{i \\theta}.$$ "
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "slideshow": {
     "slide_type": "subslide"
    }
   },
   "source": [
    "Here $|\\xi_j|$ is the modulus (or magnitude) or the complex number and is defined as $|\\xi_j| = x^2 + y^2$ where $\\xi_j = x + i j$.  If the $\\xi_j$s are on the boundary of the absolute stability region then we know that $|\\xi_j| = 1$.  Using this in conjunction with the stability polynomial then leads to \n",
    "\n",
    "$$\\rho(e^{i\\theta}) - z \\sigma(e^{i\\theta}) = 0$$\n",
    "\n",
    "which solving for $z$ leads to\n",
    "\n",
    "$$z(\\theta) = \\frac{\\rho(e^{i\\theta})}{\\sigma(e^{i\\theta})}.$$"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "As an example consider the Adams-Bashforth 2-stage method.  The stability polynomial can be found as\n",
    "$$\\begin{aligned}\n",
    "    U_{n+2} &= U_{n+1} + \\frac{\\Delta t}{2} (-f(U_n) + 3 f(U_{n+1})) \\\\\n",
    "    \\pi(\\xi, z) &= U_{n+2} - U_{n+1} - \\frac{\\Delta t}{2} (-f(U_n) + 3 f(U_{n+1})) = 0 \\\\\n",
    "    &= U_{n+2} - U_{n+1} - \\frac{1}{2} (\\Delta t \\lambda U_n - 3 \\Delta t \\lambda U_{n+1}) \\\\\n",
    "    &= 2 \\xi^2 - 2 \\xi + 3 z\\xi - z \\\\\n",
    "    &= \\rho(\\xi, z) + z \\sigma(\\xi, z)\n",
    "\\end{aligned}$$\n",
    "where\n",
    "$$\n",
    "    \\rho(\\xi, z) = 2 ( \\xi - 1) \\xi ~~~ \\text{and} ~~~ \\sigma(\\xi, z) = 3 \\xi - 1\n",
    "$$\n",
    "so that\n",
    "$$\n",
    "    z(\\theta) = \\frac{2 (\\xi - 1) \\xi}{3 \\xi - 1}.\n",
    "$$"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "slideshow": {
     "slide_type": "subslide"
    }
   },
   "source": [
    "This does not necessarily ensure that given a $\\theta$ that $z(\\theta)$ will lie on the absolute stability region's boundary.  This can occur when $\\xi_j = 1$ but to the left and right of the curve $\\xi_j > 1$ and so therefore does not mark the boundary of the region.  To determine whether a particular region outlined by this curve is inside or outside of the stability region we can evaluate all the roots of $\\pi(\\xi, z)$ at some $z$ inside of the region in question and see if then $\\forall j, \\xi_j < 1$."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "slideshow": {
     "slide_type": "subslide"
    }
   },
   "source": [
    "For one-step methods this becomes easier, if we look at the ratio $R(z)$ we defined earlier as\n",
    "$$\n",
    "    U_{n+1} = R(z) U_n\n",
    "$$\n",
    "in the case of the pth order Taylor series method applied to $u'(t) = \\lambda u$ we get\n",
    "\n",
    "$$\\begin{aligned}\n",
    "    U_{n+1} &= U_n + \\Delta t \\lambda U_n + \\frac{1}{2}\\Delta t^2 \\lambda^2 U_n + \\cdots + \\frac{1}{p!}\\Delta t^p \\lambda^p U_n \\\\\n",
    "    &=\\left(1 + z + \\frac{1}{2} z^2 + \\frac{1}{6} z^3 + \\cdots +\\frac{1}{p!}z^p\\right) U_n \\Rightarrow \\\\\n",
    "    R(z) &= 1 + z + \\frac{1}{2} z^2 + \\frac{1}{6} z^3 + \\cdots +\\frac{1}{p!}z^p.\n",
    "\\end{aligned}$$"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "slideshow": {
     "slide_type": "subslide"
    }
   },
   "source": [
    "Setting $R(z) = e^{i\\theta}$ could lead to a way for solving for the boundary but (where $|R(z)| = 1$) but this is very difficult to do in general.  Instead if we plot the contours of $R(z)$ in the complex plane we can pick out the $R(z)=1$ contour and plot that."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "scrolled": false,
    "slideshow": {
     "slide_type": "skip"
    }
   },
   "outputs": [],
   "source": [
    "theta = numpy.linspace(0.0, 2.0 * numpy.pi, 100)\n",
    "\n",
    "# ==================================\n",
    "#  Forward euler\n",
    "fig = plt.figure()\n",
    "fig.set_figwidth(fig.get_figwidth() * 2)\n",
    "fig.set_figheight(fig.get_figheight() * 2)\n",
    "axes = fig.add_subplot(2, 2, 1)\n",
    "x = numpy.linspace(-2, 2, 100)\n",
    "y = numpy.linspace(-2, 2, 100)\n",
    "X, Y = numpy.meshgrid(x, y)\n",
    "Z = X + 1j * Y\n",
    "\n",
    "# Evaluate which regions are actually in stability region\n",
    "print(\"Forward Euler's method - Stability region tests\")\n",
    "z = -1 + 1j * 0\n",
    "print(\"  Inside of circle: \", numpy.abs(1.0 + z))\n",
    "z = -3 + 1j * 0\n",
    "print(\"  Outside of circle: \", numpy.abs(1.0 + z))\n",
    "\n",
    "axes.contour(X, Y, numpy.abs(1.0 + Z), levels=[1.0])\n",
    "axes.plot(x, numpy.zeros(x.shape),'k')\n",
    "axes.plot(numpy.zeros(y.shape), y,'k')\n",
    "axes.set_aspect('equal')\n",
    "axes.set_title(\"Forward Euler\")\n",
    "\n",
    "# ==================================\n",
    "#  Backwards Euler\n",
    "axes = fig.add_subplot(2, 2, 2)\n",
    "x = numpy.linspace(-2, 2, 100)\n",
    "y = numpy.linspace(-2, 2, 100)\n",
    "X, Y = numpy.meshgrid(x, y)\n",
    "Z = X + 1j * Y\n",
    "\n",
    "# Evaluate which regions are actually in stability region\n",
    "print(\"Backward Euler's method - Stability region tests\")\n",
    "z = 1 + 1j\n",
    "print(\"  Inside of circle: \", numpy.abs(1.0 / (1.0 - z)))\n",
    "z = -1 + 1j\n",
    "print(\"  Outside of circle: \", numpy.abs(1.0 / (1.0 - z)))\n",
    "\n",
    "axes.contour(X, Y, numpy.abs(1.0 / (1.0 - Z)), levels=[1.0])\n",
    "axes.plot(x, numpy.zeros(x.shape),'k')\n",
    "axes.plot(numpy.zeros(y.shape), y,'k')\n",
    "axes.set_aspect('equal')\n",
    "axes.set_title(\"Backwards Euler\")\n",
    "\n",
    "# ==================================\n",
    "#  Taylor series method of order 4\n",
    "axes = fig.add_subplot(2, 2, 3)\n",
    "x = numpy.linspace(-5, 5, 100)\n",
    "y = numpy.linspace(-5, 5, 100)\n",
    "X, Y = numpy.meshgrid(x, y)\n",
    "Z = X + 1j * Y\n",
    "\n",
    "# Evaluate which regions are actually in stability region\n",
    "print(\"Taylor series method of order 4 - Stability region tests\")\n",
    "z = -1 + 1j * 0\n",
    "print(\"  Inside of strange region: \", numpy.abs(1 + z + 0.5 * z**2 + 1.0/6.0 * z**3 + 1.0 / 24.0 * z**4))\n",
    "z = -4 + 1j * 0\n",
    "print(\"  Outside of strange region: \", numpy.abs(1 + z + 0.5 * z**2 + 1.0/6.0 * z**3 + 1.0 / 24.0 * z**4))\n",
    "\n",
    "axes.contour(X, Y, numpy.abs(1 + Z + 0.5 * Z**2 + 1.0/6.0 * Z**3 + 1.0 / 24.0 * Z**4), levels=[1.0])\n",
    "axes.plot(x, numpy.zeros(x.shape),'k')\n",
    "axes.plot(numpy.zeros(y.shape), y,'k')\n",
    "axes.set_aspect('equal')\n",
    "axes.set_title(\"4th Order Taylor Series\")\n",
    "\n",
    "# ==================================\n",
    "# 2-step Adams-Bashforth\n",
    "theta = numpy.linspace(0.0, 2.0 * numpy.pi, 1000)\n",
    "xi = numpy.exp(1j * theta)\n",
    "\n",
    "rho_2AB = lambda xi: 2.0 * (xi - 1.0) * xi\n",
    "sigma_2AB = lambda xi: 3.0 * xi - 1.0\n",
    "z_2AB = rho_2AB(xi) / sigma_2AB(xi)\n",
    "z = rho_2AB(xi) / sigma_2AB(xi)\n",
    "\n",
    "axes = fig.add_subplot(2, 2, 4)\n",
    "axes.plot(z_2AB.real, z_2AB.imag, 'k')\n",
    "axes.plot(x, numpy.zeros(x.shape),'k')\n",
    "axes.plot(numpy.zeros(y.shape), y,'k')\n",
    "axes.set_title(\"2-step Adams-Bashforth\")\n",
    "axes.set_aspect('equal')\n",
    "axes.set_xlim([-2, 3])\n",
    "axes.set_ylim([-2, 2])\n",
    "\n"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.1"
  },
  "latex_envs": {
   "bibliofile": "biblio.bib",
   "cite_by": "apalike",
   "current_citInitial": 1,
   "eqLabelWithNumbers": true,
   "eqNumInitial": 0
  }
 },
 "nbformat": 4,
 "nbformat_minor": 1
}
